package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.dto.InTaskDto;
import org.jeecg.modules.dto.OutTaskRunningDto;
import org.jeecg.modules.service.IActTaskService;
import org.jeecg.modules.utils.RestMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zjialin<br>
 * @version 1.0<br>
 * @createDate 2019/08/30 11:59 <br>
 * @Description <p> 任务相关接口 </p>
 */

@RestController
@Api(tags = "待办任务")
@RequestMapping("/act/task/todo")
@Slf4j
public class ActTaskRunningController extends BaseController {

    @Autowired
    private IActTaskService service;

    @GetMapping(path = "listPage")
    @ApiOperation(value = "分页查询待办任务", notes = "根据流程assignee查询assignee的待办")
    public Result<IPage<OutTaskRunningDto>> listPage(InTaskDto inTaskDto, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        try {
            IPage<OutTaskRunningDto> list = service.listTodoTask(inTaskDto, pageNo, pageSize, req);
            return Result.OK(list);
        } catch (Exception e) {
            return Result.error("查询失败" + e.getMessage());
        }
    }


    @PostMapping(path = "completeTask")
    @ApiOperation(value = "完成任务", notes = "完成任务，任务进入下一个节点")
    @ApiImplicitParams({@ApiImplicitParam(name = "taskId", value = "任务ID", dataTypeClass = String.class, paramType = "query"), @ApiImplicitParam(name = "variables", value = "填充参数", dataType = "body", paramType = "query"),})
    public Result<?> completeTask(@RequestParam("taskId") String taskId, Map<String, Object> variables) {
        try {
            taskService.complete(taskId, variables);
            return Result.OK("完成任务成功", taskId);
        } catch (Exception e) {
            log.error("完成任务，异常:{}", e.getMessage());
            return Result.error("完成任务失败", e.getMessage());
        }
    }

    @GetMapping(path = "taskClaim")
    @ApiOperation(value = "任务签收", notes = "任务签收，把组任务签收到自己")
    @ApiImplicitParams({@ApiImplicitParam(name = "taskId", value = "任务ID", dataTypeClass = String.class, paramType = "query"),})
    public Result<?> taskClaim(@RequestParam("taskId") String taskId, HttpServletRequest request) {
        RestMessage restMessage = new RestMessage();
        try {
            String loginUsername = JwtUtil.getUserNameByToken(request);
            taskService.setAssignee(taskId, loginUsername);
            return Result.OK("任务签收成功", taskId);
        } catch (Exception e) {
            log.error("任务转办，异常:{}", e.getMessage());
            return Result.error("任务签收失败", e.getMessage());
        }
    }

    @GetMapping(path = "taskTransfer")
    @ApiOperation(value = "任务转办", notes = "任务转办，把任务交给别人处理")
    @ApiImplicitParams({@ApiImplicitParam(name = "taskId", value = "任务ID", dataTypeClass = String.class, paramType = "query"), @ApiImplicitParam(name = "userKey", value = "用户Key", dataTypeClass = String.class, paramType = "query"),})
    public Result<?> taskTransfer(@RequestParam("taskId") String taskId, @RequestParam("userKey") String userKey) {
        try {
            taskService.setAssignee(taskId, userKey);
            return Result.OK("任务转办成功", taskId);
        } catch (Exception e) {
            log.error("任务转办,异常:{}", e.getMessage());
            return Result.error("完成任务失败", e.getMessage());
        }
    }
}